﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using BusinessEntities;
using System.Reflection;

namespace DataAccess
{
    public class CSVGenerator
    {
        private const string _fileFullPath = "Revenant Members.csv";

        public static void GenerateCsvReport(List<Raider> members)
        {
            try
            {
                //Declaration
                StreamWriter writer;
                StringBuilder line;
                StringBuilder fullText;
                string propertyValue;

                //Initialization
                writer = PrepareStream();
                fullText = new StringBuilder();

                //Write header line
                Raider headerRaider = new Raider();
                line = new StringBuilder();
                foreach (PropertyInfo property in headerRaider.GetType().GetProperties())
                {
                    //Generate the line                
                    line.Append(property.Name);
                    line.Append(",");

                    //Append the line to the object carrying the full text                
                }
                
                //Remove the last "," from the line
                line.Remove(line.Length - 1, 1);

                fullText.AppendLine(line.ToString());

                //Loop through the collection members (Rows)
                foreach (Raider member in members)
                {
                    line = new StringBuilder();
                    //Loop through the collection fields (Columns)
                    foreach (PropertyInfo property in member.GetType().GetProperties())
                    {
                        //Generate the line
                        propertyValue = property.GetValue(member, null).ToString();
                        line.Append(propertyValue);                      
                        line.Append(",");
                    }

                    //Remove the last "," from the line
                    line.Remove(line.Length - 1, 1);

                    //Append the line to the object carrying the full text
                    fullText.AppendLine(line.ToString());
                }

                //Write the text to the underlying stream
                writer.Write(fullText.ToString());

                //Close and dispose objects
                writer.Close();
                writer.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private static StreamWriter PrepareStream()
        {
            FileStream stream;
            StreamWriter writer = null;

            //try
            //{
            stream = File.Create(_fileFullPath);
            writer = new StreamWriter(stream, Encoding.Default);
            //}
            //catch (UnauthorizedAccessException unauthorized)
            //{
            //    new UnauthorizedAccessException("You aren’t authorize to access (if exists) or create this file: " + unauthorized.Message);
            //}

            //catch (PathTooLongException toolong)
            //{
            //    new PathTooLongException("The name of the file is too long: " + toolong.Message);
            //}

            //catch (ArgumentException invalidpath)
            //{
            //    new ArgumentException("Invalid path for the file: " + invalidpath.Message);
            //}

            //catch (Exception general)
            //{
            //    new Exception("An exception ocurred: " + general.Message);
            //}

            return writer;
        }
    }
}